<?php
// $Id: waimai.php 257 2012-04-12 12:56:59Z tomqin@gmail.com $

class Waimai_Controller extends FrontController{

	var $params = array();

	function init(){
		$this->zone = Base::loadModule('zone', true);
		$this->zone->initCity();
		$this->setVar('zone', $this->zone);
	}

	function _onDefault(){
		$this->setVar('action', $this->action);
		$currZone = $this->zone->getZoneByPy($this->action);
		if($currZone->parent_id == 0){
			$selBigZoneId = $currZone->zone_id;
			$selSubZoneId = 0;
		}else{
			$selBigZoneId = $currZone->parent_id;
			$selSubZoneId = $currZone->zone_id;
		}

		$pagesize = 10;
		$params = $this->parseParam();

		$query  = str_replace(array('"',"%","&","'"), '', $_REQUEST['q']);
		$page   = $_REQUEST['page'] = $params['p'];
		$result = $this->search($selBigZoneId, $selSubZoneId, $page, $pagesize, $query);
		$total  = $result[0]['total'];

		Base::loadLibrary('Pager');
		$pager  = new Pager($total, $pagesize);

		$urltpl = Base::frontUrl($this->controller, $this->action, 'p{PAGE}?q='.urlencode($query));

		$bigzone  = $this->zone->getZoneById($selBigZoneId)->name;
		$subzone  = $this->zone->getZoneById($selSubZoneId)->name;
		$title   = "大连{$subzone}外卖信息_大连{$bigzone}外卖送餐_网上订餐";

		$this->seoTitle($title);
		$this->setVar('pagestring', $pager->frontPageLink($urltpl));
		$this->setVar('result', $result);
		$this->setVar('query', $query);
		$this->setVar('big_zone_id', $selBigZoneId);
		$this->setVar('sub_zone_id', $selSubZoneId);
		$this->render('waimai-search');
	}

	function search($big_zone_id, $sub_zone_id, $page, $pagesize = 5, $query = ''){
		$big_zone_id = intval($big_zone_id);
		$sub_zone_id = intval($sub_zone_id);

		$result = DB::getAll("call sp_search_store({$pagesize}, {$page}, {$big_zone_id}, {$sub_zone_id}, '{$query}')");
		return $result;
	}

	private function parseParam(){
		$param = explode('-', $this->param);
		$array = array();
		foreach($param as $var){
			$array[substr($var, 0, 1)] = substr($var, 1);
		}
		$array['p'] = array_get_int($array, 'p', 1);
		return $array;
	}

	function __destruct(){
		//print_r($this->db);
	}
}
?>